package com.ruoyi.tea.api;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.tea.domain.TeaUser;
import com.ruoyi.tea.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 小程序个人统计API
 *
 * @author wangfeng
 * @time 2022年08月07日18:12:30
 */
@Controller
@RequestMapping("/wx/api/report")
public class UserCountApiController {

    @Autowired
    private ITeaUserService iTeaUserService;

    @Autowired
    private ITeaSeeMediaService iTeaSeeMediaService;

    @Autowired
    private ITeaSeeLiveService iTeaSeeLiveService;

    @Autowired
    private ITeaFollowRecoringService iTeaFollowRecoringService;

    @Autowired
    private ITeaFollowRoomService iTeaFollowRoomService;

    @RequestMapping("/watch/count")
    @ResponseBody
    public AjaxResult watchCount(@RequestBody TeaUser teaUser) {
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(teaUser);
        if (teaUserList == null || teaUserList.size() == 0) {
            return AjaxResult.error("未登录");
        }
        Map<String, Object> params = new HashMap<>(1);
        params.put("openId", teaUser.getOpenId());
        Long seeLive = iTeaSeeLiveService.seeDuration(params);
        Long seeRecording = iTeaSeeMediaService.seeDuration(params);
        JSONObject res = new JSONObject();
        res.put("watchTatalDuration", (seeLive + seeRecording) / 3600);
        res.put("collection",iTeaFollowRecoringService.followNumber(teaUser));
        res.put("follow",iTeaFollowRoomService.followNumber(teaUser));
        return AjaxResult.success(res);
    }

}
